/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetmealGroupMapper;
import com.itheima.health.dao.SetmealMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.SetmealCheckgroup;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

/**
 * <dl>
 * <dd>描述: ~ SetmealServiceImpl</dd>
 * <dd>创建时间：  11:42 2021/1/8</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/1/8      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@DubboService
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealGroupMapper setmealGroupMapper;

    /**
     * @Author: guodong
     * @Date: 11:42 2021/1/8
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.PageResult
     * @Description: 检查套餐-分页查询
    */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[检查套餐-分页查询]data: {}", queryPageBean);
        //1.分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.根据条件查询
        Example example = new Example(Setmeal.class);
        //3.构造查询条件
        Example.Criteria criteria = example.createCriteria();
        //4.获取前段传递的查询条件
        String queryString = queryPageBean.getQueryString();
        //5.判断不为空
        if(!StringUtils.isEmpty(queryString)){
            //模糊 or
            criteria.orLike("name", "%" + queryString +"%");
            criteria.orLike("code", "%" + queryString +"%");
            criteria.orLike("helpCode", "%" + queryString +"%");
        }
        Page<Setmeal> page = (Page<Setmeal>) setmealMapper.selectByExample(example);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * @Author: guodong
     * @Date: 15:14 2021/1/8
     * @Parms [setmeal, checkgroupIds]
     * @ReturnType: void
     * @Description: 新增套餐-保存数据
    */
    @Override
    @Transactional //事务注解
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[新增套餐-保存数据]setmeal: {}, checkgroupIds: {}", setmeal, checkgroupIds);
        //1.保存套餐
        setmealMapper.insertSelective(setmeal);
        //2.遍历checkgroupIds 保存中间关联关系 表
        SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
        setmealCheckgroup.setSetmealId(setmeal.getId());
        for (Integer checkgroupId : checkgroupIds) {
            setmealCheckgroup.setCheckgroupId(checkgroupId);
            setmealGroupMapper.insertSelective(setmealCheckgroup);
        }
    }

    /**
     * @Author: guodong
     * @Date: 10:00 2021/1/9
     * @Parms [fileName]
     * @ReturnType: int
     * @Description: 根据文件名称查询
     */
    @Override
    public int count(String fileName) {
        Setmeal setmeal = new Setmeal();
        setmeal.setImg(fileName);
        return setmealMapper.selectCount(setmeal);
    }
}
